//
//  MovieList9ViewController.m
//  UI-豆瓣项目APP
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 孙鹏. All rights reserved.
//

#import "MovieList9ViewController.h"
#import "AppTools.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "MovieDetalViewController.h"
@interface MovieList9ViewController ()<MovieList9Delegete>
@property(nonatomic,retain)MBProgressHUD *hud;
@end

@implementation MovieList9ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIScrollView *movieList9SC = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    
    movieList9SC.backgroundColor = [UIColor whiteColor];
    
    
    [self.view addSubview:movieList9SC];
    self.allKeys = [NSMutableArray array];
    
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hud show:YES];
    self.hud.labelText = @"正在加载中.....";
    self.view.backgroundColor = [UIColor yellowColor];
    NSString *url = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    self.i = 0;
    [AppTools getDataWithFileURL:url block:^(NSMutableDictionary *result) {
        
        
        NSMutableArray *array = [result objectForKey:@"result"];
        
        for (NSMutableDictionary *dic in array) {
            
            Movielist *movie = [[Movielist alloc]init];
            
            [movie setValuesForKeysWithDictionary:dic];
            
            
            [self.allKeys addObject:movie];
            
            MovieList9View *movieList9 = [[MovieList9View alloc]initWithFrame:CGRectMake(20 + 118 * ((self.i)%3),20+ 180 * (self.i/3) ,100,  300)];
            movieList9.headImage.tag = 1000 + self.i;
            self.i = self.i + 1;
            
            NSString *movieId = [dic objectForKey:@"pic_url"];
            
            NSURL *url =  [NSURL URLWithString:movieId];
            
           
            [movieList9.headImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHoderImage"]];
            movieList9.movieName.text = movie.movieName;
          [movieList9SC addSubview:movieList9];
            [movieList9 release];            
            
            movieList9.delegete = self;
        }
        
        movieList9SC.contentSize = CGSizeMake(375, (self.allKeys.count/3 + 1) * 180 + 20);
        [self.hud hide:YES];

    }];
    
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(popMovieList)];
    
    
    // Do any additional setup after loading the view.
}

- (void)popMovieList
{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}

- (void)tagToMovieListController:(NSInteger)index
{
    
    
     Movielist *movie = [self.allKeys objectAtIndex:index];
    MovieDetalViewController *movieDVC = [[MovieDetalViewController alloc]init];
    
    
    movieDVC.amovie = movie;
    NSLog(@"----%@",movieDVC.amovie);
    [self.navigationController pushViewController:movieDVC animated:YES];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
